

package com.zmks.jkgj.xuexiao.controller;




import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.zmks.jkgj.core.controller.ABaseController;
import com.zmks.jkgj.core.resp.Resp;
import com.zmks.jkgj.xuexiao.SessionManager;
import com.zmks.jkgj.xuexiao.resp.WebResp;
import com.zmks.jkgj.xuexiao.service.ICrctConfigGradeService;
import com.zmks.jkgj.xuexiao.service.ICrctObjClassService;
import com.zmks.jkgj.xuexiao.service.ICrctObjParentService;
import com.zmks.jkgj.xuexiao.service.ICrctObjTeacherService;
import com.zmks.jkgj.xuexiao.service.ICrctRelClassTeacherService;
import com.zmks.jkgj.xuexiao.view.Grade_Class_Teacher_View;
import com.zmks.jkgj.xuexiao.vo.CrctConfigGradeVO;
import com.zmks.jkgj.xuexiao.vo.CrctObjClassVO;
import com.zmks.jkgj.xuexiao.vo.CrctObjGradeVO;
import com.zmks.jkgj.xuexiao.vo.CrctObjParentVO;
import com.zmks.jkgj.xuexiao.vo.CrctObjTeacherVO;
import com.zmks.jkgj.xuexiao.vo.CrctRelClassTeacherVO;
/**
 * 班级管理
 * @author jiangyaning
 * 
 *
 */
@Controller
public class CrctObjClassController extends ABaseController {
    @Autowired
	
	private ICrctObjClassService crctObjClassService;	
   
     @Autowired
	
	private ICrctRelClassTeacherService crctRelClassTeacherService;	
     @Autowired
     private ICrctConfigGradeService crctConfigGradeService;
    
    
    
	 //查询
	@RequestMapping("/get_CrctObjClass_list")
	@ResponseBody
	public Resp get_CrctObjClass_list( Integer pageIndex,
			Integer pageSize,HttpServletRequest request) {
		WebResp 	webResp = new WebResp();
	   CrctObjClassVO crctObjClassVO=new  CrctObjClassVO();
		webResp.setData(crctObjClassService.getList(crctObjClassVO));
		webResp.setTotalCount(crctObjClassService.getCount(crctObjClassVO));
	
		return webResp;
	}
	
	
	
	 //增加
	@RequestMapping("/save_CrctObjClass")
	@ResponseBody
	public Resp save_CrctObjClass(String name,String level, String grade_oid,
			String num,String school_oid ,HttpServletRequest request) {
		WebResp 	webResp = new WebResp();
	   CrctObjClassVO crctObjClassVO=new  CrctObjClassVO();
	   crctObjClassVO.setName(name);
	   crctObjClassVO.setLevel(level);
	   crctObjClassVO.setNum(Long.parseLong(num));
	   crctObjClassVO.setSchool_oid(Long.parseLong(school_oid));
	   crctObjClassVO.setGrade_oid(Long.parseLong(grade_oid));
	   crctObjClassVO.setCreate_date(new Date());
	   crctObjClassService.save(crctObjClassVO);
		return webResp;
	}
	 //删除
	@RequestMapping("/remove_CrctObjClass")
	@ResponseBody
	public Resp remove_CrctObjClass(Long id,HttpServletRequest request) {
		WebResp 	webResp = new WebResp();
	    //删除关联
		   CrctRelClassTeacherVO  laoshi=new CrctRelClassTeacherVO();
		   laoshi.setClass_oid(id);
		 
	 List<CrctRelClassTeacherVO>  renkelaoshi_list_del=  crctRelClassTeacherService.getList(laoshi);
		 for(int i=0;i<renkelaoshi_list_del.size();i++)
		 {
			 crctRelClassTeacherService.remove(renkelaoshi_list_del.get(i).getId()); 
		 }
		
		//删除当前
		
		crctObjClassService.remove(id);
		return webResp;
	}
	
//-----------------------------------------------------------------------------------
    /**
     * 学校管理者
     * @param s_id
     * @param pageIndex
     * @param pageSize
     * @param request
     * @return
     */
	@RequestMapping("/get_CrctObjClass_list_teacher")
	@ResponseBody
	public Resp get_CrctObjClass_list_teacher(
			String s_id,			
			Integer pageIndex,
			
			Integer pageSize,HttpServletRequest request) {
		WebResp 	webResp = new WebResp();
	   CrctObjClassVO crctObjClassVO=new  CrctObjClassVO();
	   String  school_oid = SessionManager.getsession(request).getSchool_oid();
	   webResp.setData(crctObjClassService.getView(school_oid));
		webResp.setTotalCount(crctObjClassService.getCount(crctObjClassVO));
	    return webResp;
	}
	
	

	 //增加
	/**
	 * ids 1,2,3,3分开
	 */
	@RequestMapping("/save_CrctObjClass_teacher")
	@ResponseBody
	public Resp save_CrctObjClass_teacher(String name,String level, String grade_oid,
			String num ,Long teacher_oid,String listSting,HttpServletRequest request) {
		WebResp 	webResp = new WebResp();
		try{
			
			if(name=="")
			{
				webResp.setRet(1);
			webResp.setErrcode(0);
			webResp.setMsg("请填写班级名称！");
			return webResp;
			}
			if(num=="")
			{
			webResp.setRet(1);
			webResp.setErrcode(0);
			webResp.setMsg("请填写班级编号！");
			return webResp;
			}
	  
		//增加班级
		CrctObjClassVO crctObjClassVO=new  CrctObjClassVO();
	   crctObjClassVO.setName(name);
	   crctObjClassVO.setLevel(level);
	   crctObjClassVO.setNum(Long.parseLong(num));
	   String  school_oid = SessionManager.getsession(request).getSchool_oid();
	   crctObjClassVO.setSchool_oid(Long.parseLong(school_oid.trim()));
	   crctObjClassVO.setGrade_oid(Long.parseLong(grade_oid.trim()));
	   crctObjClassVO.setCreate_date(new Date());
	   CrctObjClassVO c=  crctObjClassService.save(crctObjClassVO);
	   //增加班主任
	   CrctRelClassTeacherVO  banzhuren=new CrctRelClassTeacherVO();
	   banzhuren.setClass_oid(c.getOid());
	   banzhuren.setTeacher_oid(teacher_oid);
	 
	   banzhuren.setPosition_oid(0L);
	   banzhuren.setCreate_date(new Date());
	   crctRelClassTeacherService.save(banzhuren);
     //增加任课老师
	   if(listSting!=null&&listSting!=""){
     String[] list=listSting.split(",");
	   for(int i=0;i<list.length;i++)
	   { 
		   CrctRelClassTeacherVO  teacher=new CrctRelClassTeacherVO();
		   banzhuren.setClass_oid(c.getOid());
		   banzhuren.setTeacher_oid(Long.parseLong(list[i]));
		   banzhuren.setPosition_oid(1L);
		   banzhuren.setCreate_date(new Date());
		   crctRelClassTeacherService.save(banzhuren); 
	   }
	   }
		  } catch (Exception e) {
				e.printStackTrace();
				webResp.setRet(1);
				webResp.setErrcode(0);
				webResp.setMsg("失败！");
				return webResp;
			}
		return webResp;
	}
	
	  /**
     * 修改班级
     * @param s_id
     * @param pageIndex
     * @param pageSize
     * @param request
     * @return
     */
	@RequestMapping("/get_CrctObjClass_byclassid")
	@ResponseBody
	public Resp get_CrctObjClass_byclassid(String classId,HttpServletRequest request) {
		WebResp 	webResp = new WebResp();
		try{
	
		HashMap map=new HashMap();
		CrctObjClassVO crctObjClassVO=	crctObjClassService.get(Long.parseLong(classId.trim()));
		//获得年纪
		CrctConfigGradeVO  crctConfigGradeVO=	crctConfigGradeService.get(crctObjClassVO.getGrade_oid());
		map.put("grade_oid", crctConfigGradeVO.getOid());
		map.put("gradeName", crctConfigGradeVO.getName());
		//获得年份
		map.put("level",crctObjClassVO.getLevel().trim() );
		//获得班级名称
		map.put("name",crctObjClassVO.getName() );
		//班级编号
		map.put("num",crctObjClassVO.getNum() );
		Grade_Class_Teacher_View	 v=crctObjClassService.getView(Long.parseLong(classId.trim()));
	    //获得班主任(ID,姓名)
		    //----关联表获得老师的ID，id再获得老师的信息
		map.put("banzhuren_oid",v.getTeacherOid() );
		map.put("banzhuren_Name",v.getTeacherName() );
	
		//获得认可老师（ID,姓名）
		webResp.setData(map);
		  } catch (Exception e) {
				e.printStackTrace();
				webResp.setRet(1);
				webResp.setErrcode(0);
				webResp.setMsg("失败！");
				return webResp;
			}
		  return webResp;
	}
	
	 //增加
	/**
	 * ids 1,2,3,3分开
	 */
	@RequestMapping("/update_CrctObjClass_teacher")
	@ResponseBody
	public Resp update_CrctObjClass_teacher(Long class_oid,String name,String level, String grade_oid,
			String num ,Long teacher_oid,String listSting,HttpServletRequest request) {
		WebResp 	webResp = new WebResp();
		try{
			
			if(name=="")
			{
				webResp.setRet(1);
			webResp.setErrcode(0);
			webResp.setMsg("请填写班级名称！");
			return webResp;
			}
			if(num=="")
			{
			webResp.setRet(1);
			webResp.setErrcode(0);
			webResp.setMsg("请填写班级编号！");
			return webResp;
			}
	  
		//增加班级
		CrctObjClassVO crctObjClassVO=crctObjClassService.get(class_oid);
	   crctObjClassVO.setName(name);
	   crctObjClassVO.setLevel(level);
	   crctObjClassVO.setNum(Long.parseLong(num));
	   String  school_oid = SessionManager.getsession(request).getSchool_oid();
	   crctObjClassVO.setSchool_oid(Long.parseLong(school_oid.trim()));
	   crctObjClassVO.setGrade_oid(Long.parseLong(grade_oid.trim()));
	   crctObjClassVO.setCreate_date(new Date());
	    crctObjClassService.save(crctObjClassVO);
	  
	   CrctRelClassTeacherVO  banzhuren1=new CrctRelClassTeacherVO();
	   banzhuren1.setClass_oid(class_oid);
	   banzhuren1.setPosition_oid(0L);
 List<CrctRelClassTeacherVO>  list_del=  crctRelClassTeacherService.getList(banzhuren1);
	 for(int i=0;i<list_del.size();i++)
	 {
		 crctRelClassTeacherService.remove(list_del.get(i).getId()); 
	 }
 
	   //ID为空删除班主任
	   if(teacher_oid!=null)
	   {
		   CrctRelClassTeacherVO  banzhuren=new CrctRelClassTeacherVO();
		   banzhuren.setClass_oid(crctObjClassVO.getOid());
		   banzhuren.setTeacher_oid(teacher_oid);
		 
		   banzhuren.setPosition_oid(0L);
		   banzhuren.setCreate_date(new Date());
		   crctRelClassTeacherService.save(banzhuren);
		 
	   }
	  
     //删除任课老师
	   
	   CrctRelClassTeacherVO  renkelaoshi=new CrctRelClassTeacherVO();
	   banzhuren1.setClass_oid(class_oid);
	   banzhuren1.setPosition_oid(1L);
 List<CrctRelClassTeacherVO>  renkelaoshi_list_del=  crctRelClassTeacherService.getList(banzhuren1);
	 for(int i=0;i<renkelaoshi_list_del.size();i++)
	 {
		 crctRelClassTeacherService.remove(renkelaoshi_list_del.get(i).getId()); 
	 }
	 
	
	   //增加任课老师
	   if(listSting!=null&&listSting!=""){
     String[] list=listSting.split(",");
	   for(int i=0;i<list.length;i++)
	   { 
		   CrctRelClassTeacherVO  teacher=new CrctRelClassTeacherVO();
		   teacher.setClass_oid(crctObjClassVO.getOid());
		   teacher.setTeacher_oid(Long.parseLong(list[i]));
		   teacher.setPosition_oid(1L);
		   teacher.setCreate_date(new Date());
		   crctRelClassTeacherService.save(teacher); 
	   }
	   }
		  } catch (Exception e) {
				e.printStackTrace();
				webResp.setRet(1);
				webResp.setErrcode(0);
				webResp.setMsg("失败！");
				return webResp;
			}
		return webResp;
	}
	
	@RequestMapping("/get_CrctObjClass_list_byschool")
	@ResponseBody
	public Resp get_CrctObjClass_list_byschool(
			HttpServletRequest request) {
		WebResp 	webResp = new WebResp();
		  CrctObjClassVO crctObjClassVO=new  CrctObjClassVO();
		  String  school_oid = SessionManager.getsession(request).getSchool_oid();
		  crctObjClassVO.setSchool_oid(Long.parseLong(school_oid));
		  webResp.setData(crctObjClassService.getList(crctObjClassVO));
	 
	
		return webResp;
	}

}
